package edu.cque.jt.common.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author Hua
 * @create 2022-07-21 18:00
 * 基于ScheduledExecutorService对象完成一个多线程的任务调度,在nacos注册中心发送定时心跳以及nacos配置中心数据定时拉取(pull),底层定时通过这个对象完成的.
 */
public class ScheduledExecutorServiceUtils {
    public static void main(String[] args) {
        Schedule01();

    }

    public static void Schedule01() {
        // 注意此处线程个数为1
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
        long start = System.currentTimeMillis();
        executorService.scheduleAtFixedRate(() -> {
            System.out.println("第一次提交");
            System.out.println(System.currentTimeMillis() - start);
            try {
                // 注意此处休眠4秒
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, 1, 3, TimeUnit.SECONDS);
        executorService.scheduleAtFixedRate(() -> {
            System.out.println("第二次提交");
            System.out.println(System.currentTimeMillis() - start);
        }, 1, 3, TimeUnit.SECONDS);
    }

    public static void Schedule02() {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(3);


    }


}

